package com.lingyuan.discounts.controller;


import com.alibaba.fastjson.JSON;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.cart.CartItemVo;
import com.lingyuan.common.core.domain.vo.sms.ComputeVo;
import com.lingyuan.common.core.domain.vo.sms.CouponMemberVo;
import com.lingyuan.common.core.domain.vo.sms.CouponSkuIdsVo;
import com.lingyuan.common.core.domain.vo.sms.CouponVo;
import com.lingyuan.discounts.domain.Coupon;
import com.lingyuan.discounts.domain.request.CouponRequest;
import com.lingyuan.discounts.service.CouponService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author: Li Hao Xuan
 * @name: DiscountController
 * @Description TODO
 */
@Log4j2
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 多条件查询优惠券列表
     * @param couponRequest
     * @return
     */
    @PostMapping("/findCouponList")
    public R<List<Coupon>> findCouponList(@RequestBody CouponRequest couponRequest){
        log.info("查询优惠券列表，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(), JSON.toJSONString(couponRequest));
        List<Coupon> list = couponService.findCouponList(couponRequest);
        log.info("查询优惠券列表，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(), JSON.toJSONString(list));
        return R.ok(list);
    }

    /**
     * 新增优惠券
     * @param coupon
     * @return
     */
    @PostMapping("/addCoupon")
    public R addCoupon(@RequestBody Coupon coupon){
        couponService.addCoupon(coupon);
        log.info("新增优惠券，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(coupon));
        return R.ok();
    }


    /**
     * 修改优惠券
     */
    @PutMapping("/updCoupon")
    public R updCoupon(@RequestBody Coupon coupon){
        couponService.updCoupon(coupon);
        log.info("修改优惠券，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(coupon));
        return R.ok();
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/delCoupon/{id}")
    public R delCoupon(@PathVariable("id") Long id){
        couponService.delCoupon(id);
        log.info("删除优惠券，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(id));
        return R.ok();
    }

    /**
     * 计算优惠券价格
     * @return
     */
    @PostMapping("/findCouponSkuIdsVo")
    public R<CouponSkuIdsVo> findCouponSkuIdsVo(@RequestBody ComputeVo computeVo){
        CouponSkuIdsVo couponSkuIdsVo = couponService.findCouponSkuIdsVo(computeVo);
        log.info("查询优惠券列表，方法:{},路径:{},参数：{}",
                request.getMethod(),request.getRequestURI(), JSON.toJSONString(couponSkuIdsVo));
        return R.ok(couponSkuIdsVo);
    }

    /**
     * 根据优惠券id查询条件
     * @param couponId
     * @return
     */
    @GetMapping("/findCouponId/{couponId}")
    public R<Coupon> findCouponId(@PathVariable("couponId") Long couponId){
        Coupon coupon = couponService.findCouponId(couponId);
        return R.ok(coupon);
    }

    /**
     * 用户端查询自己的优惠券列表
     */
    @GetMapping("/findSkuMinPrice/{skuIds}")
    public R<List<CouponMemberVo>> findSkuMinPrice(@PathVariable("skuIds") String skuIds){
        List<CouponMemberVo> couponMemberVoList = couponService.findSkuMinPrice(skuIds);
        return R.ok(couponMemberVoList);
    }

    /**
     * 修改优惠券使用数量
     * @param id
     * @return
     */
    @PutMapping("/updUseCount")
    public R updUseCount(@RequestParam("id") Long id){
        couponService.updUseCount(id);
        return R.ok();
    }
}
